package com.cs.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author Sam.Lee E-mail:lishanman2007@163.com
 * @version Created：Sep 1, 2011 2:15:57 PM
 */
public class DynamicProxy implements InvocationHandler {

	private OrderApi order;

	public OrderApi getProxyInstance(OrderApi order) {
		this.order = order;
		OrderApi orderApi = (OrderApi) Proxy.newProxyInstance(order.getClass()
				.getClassLoader(), order.getClass().getInterfaces(), this);
		return orderApi;
	}

	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		if (method.getName().startsWith("set")) {
			if (order != null && order.getOrderUser().equals(args[1])) {
				return method.invoke(order, args);
			} else {
				System.out.println(args[1] + "您没有权限修改...");
			}
		} else {
			return method.invoke(order, args);
		}
		return null;
	}

}
